//
//  ST_LoveModuleController.swift
//  btvideo
//
//  Created by mac on 2020/9/14.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit

//暂时弃用
class ST_LoveModuleController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = .white
        
        view.addSubview(scrollView)
        scrollView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(UIEdgeInsets.init(top: statusBarHeight + 44, left: 0, bottom: 0, right: 0))
        }
        
        scrollView.addSubview(root_view)
        
        
        
        root_view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
            make.width.equalTo(ScreenWidth*2)
            make.height.equalToSuperview()
        }
        
        self.addChild(receiveController)
        self.addChild(followController)
        
        
        
        root_view.addSubview(receiveController.view)
        root_view.addSubview(followController.view)
        
        
        
        receiveController.view.snp.makeConstraints { (make) in
            make.width.equalTo(ScreenWidth)
            make.height.top.bottom.left.equalToSuperview()
        }
        
        followController.view.snp.makeConstraints { (make) in
            make.width.equalTo(ScreenWidth)
            make.height.top.bottom.right.equalToSuperview()
            make.left.equalTo(receiveController.view.snp.right)
        }
        
        
        
        self.view.addSubview(loveMeBtn)
        self.view.addSubview(myFollowBtn)
        
        
        let relativeOffset = 10/375*ScreenWidth
        
        loveMeBtn.snp.makeConstraints { (make) in
            make.width.equalTo(100)
            make.height.equalTo(38)
            make.top.equalToSuperview().offset(statusBarHeight+3)
            make.right.equalTo(self.view.snp.centerX).offset(-relativeOffset)
        }
        
        myFollowBtn.snp.makeConstraints { (make) in
            make.size.top.equalTo(loveMeBtn)
            make.left.equalTo(self.view.snp.centerX).offset(relativeOffset)
        }
        
        view.addSubview(filter_btn)
        filter_btn.snp.makeConstraints { (make) in
            make.height.width.equalTo(44)
            make.left.equalToSuperview().offset(8)
            make.centerY.equalTo(loveMeBtn)
        }
        
        view.addSubview(ignore_btn)
        ignore_btn.snp.makeConstraints { (make) in
            make.height.width.equalTo(44)
            make.right.equalToSuperview().offset(-8)
            make.centerY.equalTo(loveMeBtn)
        }
        
    }
    
//    MARK: - Properties
    
    fileprivate lazy var receiveController = ST_ReceiveLoveCardsViewController()
    
    fileprivate lazy var followController = ST_MyFollowLoveViewController()
    
    fileprivate lazy var btn_selected_image:UIImage = {
       let img = UIImage.init(color: UIColor.init(hexString: "#FEC207")!, size: CGSize.init(width: 1, height: 1))
       return img
   }()
   
   fileprivate lazy var btn_normal_image:UIImage = {
       let img = UIImage.init(color: UIColor.white, size: CGSize.init(width: 1, height: 1))
       return img
   }()

    fileprivate lazy var last_btn = loveMeBtn
    
    fileprivate var baseTag = 10234
    
    fileprivate lazy var loveMeBtn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setBackgroundImage(btn_selected_image, for: UIControl.State.selected)
        control.setBackgroundImage(btn_normal_image, for: UIControl.State.normal)
        control.isSelected = true
        control.cornerRadius = 19
        control.setTitleForAllStates("收到喜欢")
        control.tag = baseTag
        control.setTitleColorForAllStates(UIColor.init(hexString: "#333333")!)
        control.titleLabel?.font = UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.semibold)
        control.setTitleColor(UIColor.white, for: UIControl.State.selected)
        control.addTarget(self, action: #selector(click_switch_controller_handle(sender:)), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    fileprivate lazy var myFollowBtn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setBackgroundImage(btn_selected_image, for: UIControl.State.selected)
        control.setBackgroundImage(btn_normal_image, for: UIControl.State.normal)
        control.cornerRadius = 19
        control.setTitleForAllStates("我喜欢的")
        control.tag = baseTag + 1
        control.setTitleColorForAllStates(UIColor.init(hexString: "#333333")!)
        control.titleLabel?.font = UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.semibold)
        control.setTitleColor(UIColor.white, for: UIControl.State.selected)
        control.addTarget(self, action: #selector(click_switch_controller_handle(sender:)), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    
    fileprivate lazy var scrollView:UIScrollView = {
       let control = UIScrollView()
        control.backgroundColor = UIColor.white
//        control.isScrollEnabled = false
        control.isPagingEnabled = true
        control.delegate = self
       return control
    }()
    
    
    fileprivate lazy var root_view:UIView = {
        let control = UIView()
        control.backgroundColor = UIColor.white
        return control
    }()
    
    
    fileprivate lazy var filter_btn:UIButton = {
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setImageForAllStates(ST_ImageUtils.imageName("首页_icon_筛选")!)
        control.addTarget(self, action: #selector(filter_loved_handle), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    fileprivate lazy var ignore_btn:UIButton = {
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setImageForAllStates(ST_ImageUtils.imageName("喜欢_icon_拒绝历史")!)
        control.addTarget(self, action: #selector(push_ignore_controller), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    fileprivate var curr_filter = ST_PairsLoveFilterModel()
    
}

extension ST_LoveModuleController{
    
    @objc fileprivate func filter_loved_handle(){
        
    }
    
    @objc fileprivate func push_ignore_controller(){
    
        self.navigationController?.pushViewController(ST_RefuseLoveViewController())
        
    }
    
    
}

//MARK: - Method
extension ST_LoveModuleController:UIScrollViewDelegate{
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        
        let  page = (scrollView.contentOffset.x + (0.5 * ScreenWidth)) / ScreenWidth
        
        if  page.int == 0{
            click_switch_controller_handle(sender: self.loveMeBtn)
        }else {
            click_switch_controller_handle(sender: self.myFollowBtn)
        }
    }
    
    @objc func click_switch_controller_handle(sender:UIButton){
        
        if sender.isSelected{
            return
        }
        
        last_btn.isSelected = false
        
        sender.isSelected = true
        
        last_btn = sender
        
        filter_btn.alpha = sender == loveMeBtn ? 1 : 0
        
        scrollView.setContentOffset(CGPoint.init(x: (sender.tag-self.baseTag).cgFloat*ScreenWidth, y: 0), animated: true)
        
    }
    
}
